package com.qujia.station.controller;


import com.alibaba.fastjson.JSONObject;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.core.domain.entity.SysUser;
import com.qujia.common.core.domain.model.LoginUser;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.station.domain.GzhFunctionConfig;
import com.qujia.station.domain.dto.FunctionConfigTemplateDto;
import com.qujia.station.enums.GzhTypeEnum;
import com.qujia.station.service.GzhFunctionConfigService;
import com.qujia.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/functionConfig")
@Slf4j
public class GzhFunctionConfigController {

    @Autowired
    private GzhFunctionConfigService gzhFunctionConfigService;

    @Autowired
    private ISysUserService userService;

    @GetMapping("/report/list")
    @ApiOperation(value = "查询报表功能配置列表",httpMethod = "GET",response = FunctionConfigTemplateDto.class)
    public AjaxResult getReportList(@RequestParam("userId") Long userId){
        if(Objects.isNull(userId)){
            return AjaxResult.error("请求参数异常");
        }

        log.info("getReportList请求参数：[{}]", JSONObject.toJSONString(userId));
        List<FunctionConfigTemplateDto> functionConfigTemplateList = gzhFunctionConfigService.selectGzhFunctionConfigList(userId);
        log.info("getReportList返回结果：[{}]", functionConfigTemplateList.size());
        return AjaxResult.success(functionConfigTemplateList);
    }

    @GetMapping("/memberCenter/list")
    public AjaxResult getMemberCenterList(Long enterpriseId){
        LoginUser current = SecurityUtils.getLoginUser();
        SysUser sysUser = current.getUser();
        if(Objects.isNull(sysUser)){
            return AjaxResult.error("查询报表功能配置失败，用户不存在");
        }
        GzhFunctionConfig gzhFunctionConfig=new GzhFunctionConfig();
        gzhFunctionConfig.setGzhType(GzhTypeEnum.MEMBERCENTER.getValue());
        gzhFunctionConfig.setEnterpriseId(sysUser.getEnterpriseId());
        if(enterpriseId!=null){
            gzhFunctionConfig.setEnterpriseId(enterpriseId);
        }
        if(gzhFunctionConfig.getEnterpriseId()==null){
            return AjaxResult.error("油企不存在");
        }
        log.info("getReportList请求参数：[{}]", JSONObject.toJSONString(gzhFunctionConfig));
        List<FunctionConfigTemplateDto> functionConfigTemplateDtos = gzhFunctionConfigService.selectGzhFunctionConfigList(gzhFunctionConfig);
        Map<Integer, List<FunctionConfigTemplateDto>> collect = functionConfigTemplateDtos.stream().collect(Collectors.groupingBy(FunctionConfigTemplateDto::getStatus));
        Map<String,List<FunctionConfigTemplateDto>> result=new HashMap<>();
        result.put("open",collect.get(1));
        result.put("close",collect.get(0));
        log.info("getReportList返回结果：[{}]", functionConfigTemplateDtos.size());
        return AjaxResult.success(result);
    }

    @PostMapping("/report/update")
    @ApiOperation(value = "修改报表小程序功能配置",httpMethod = "POST")
    public AjaxResult updateReportList(@RequestBody GzhFunctionConfig gzhFunctionConfig){
        if(Objects.isNull(gzhFunctionConfig) || Objects.isNull(gzhFunctionConfig.getId()) || Objects.isNull(gzhFunctionConfig.getUserId())){
            return AjaxResult.error("请求参数异常");
        }

        log.info("updateReportList请求参数：[{}]", JSONObject.toJSONString(gzhFunctionConfig));
        int i = gzhFunctionConfigService.updateGzhFunctionConfig(gzhFunctionConfig);
        if(i<=0){
            return AjaxResult.error("更新功能配置失败");
        }
        return AjaxResult.success("更新功能配置成功");
    }

    @GetMapping ("/memberCenter/update/{id}/{status}")
    @ApiOperation(value = "查询报表功能配置列表",httpMethod = "POST")
    public AjaxResult updateMemberCenterStatus(@PathVariable("id") Long id,@PathVariable("status") Integer status){
        if(Objects.isNull(id) || Objects.isNull(status)){
            return AjaxResult.error("请求参数异常");
        }

        log.info("updateReportList请求参数：{}_{}", id,status);
        GzhFunctionConfig gzhFunctionConfig=new GzhFunctionConfig();
        gzhFunctionConfig.setId(id);
        gzhFunctionConfig.setStatus(status);
        int i = gzhFunctionConfigService.updateMemberCenterConfig(gzhFunctionConfig);
        if(i<=0){
            return AjaxResult.error("更新功能配置失败");
        }
        return AjaxResult.success("更新功能配置成功");
    }
}
